package com.freesun.shop.aspect;

import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常通知类
 */
@RestControllerAdvice
public class WebErrorLog {
    private static Logger logger = LoggerFactory.getLogger(WebErrorLog.class);
    /**
     * 拦截运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> runtimeLog(RuntimeException e){
        //对于开发人员
        logger.error("发生运行时异常",e.getMessage());

        return  ResponseEntity.badRequest().body(e.getMessage());//给用户400的响应数据
    }

    @ExceptionHandler(AuthorizationException.class)
    public ResponseEntity<Void> authExecution(AuthorizationException e){

        logger.error("发生权限认证异常",e.getMessage());
        //返回前端,设置401状态码
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }
}
